package com.lanou.beautifulwardrobe.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.lanou.beautifulwardrobe.dbHelper.UsersDBHelper;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.view.RegularUtil;
import com.lanou.beautifulwardrobe.contants.ToastCommom;
import com.lanou.beautifulwardrobe.entity.greendao.UsersEntity;

import java.util.Random;

/**
 * Created by dllo on 15/11/4.
 */
public class RegisterActivity extends Activity implements View.OnClickListener {
    private ImageButton registerBack;
    private EditText registerUser, registerPwd, registerAgainPwd;
    private Button registerBtn;
    private UsersDBHelper helper=UsersDBHelper.getInstance();
    //  private ToastCommom toastCommom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();

    }

    private void initView() {
        registerBack = (ImageButton) findViewById(R.id.registerBack);
        registerUser = (EditText) findViewById(R.id.registerUser);
        registerPwd = (EditText) findViewById(R.id.registerPwd);
        registerAgainPwd = (EditText) findViewById(R.id.registerAgainPwd);
        registerBtn = (Button) findViewById(R.id.registerBtn);
        listener();
    }

    private void listener() {
        registerBack.setOnClickListener(this);
        registerBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //  ToastCommom.makeText(this,"返回了", Toast.LENGTH_SHORT).show();
        switch (v.getId()) {
            case R.id.registerBack:
                finish();
                break;
            case R.id.registerBtn:
                if (helper.isSaved(registerUser.getText().toString()+"")) {
                    ToastCommom.makeText(this, "用户名已存在", Toast.LENGTH_SHORT).show();
                } else {
                    if (RegularUtil.checkPassword(this, registerPwd.getText().toString())
                            && RegularUtil.checkPassward(this, registerPwd.getText().toString(),
                            registerAgainPwd.getText().toString())) {
                        UsersEntity usersEntity=new UsersEntity();
                        usersEntity.setUser(registerUser.getText().toString());
                        usersEntity.setPwd(registerPwd.getText().toString());
                        Random random=new Random();
                        int i= (int) ((Math.random()*35)+1);
                        String s="R.mipmap.taobao_head_"+i;
                        usersEntity.setHead(s);
                        helper.addUser(usersEntity);
                        finish();
                    }
                }
                break;
        }
    }
}
